var WxParse = require('../../../assets/common/wxParse/wxParse.js');
const App = getApp()
Page({
  data: {
    doommData: [],
    defualtZan: '../../../assets/images/user_zan.png',
    selectZan: '../../../assets/images/user_zan_select.png',
    tableList: ['功效标签', '属性信息'],
    colorList: ['#F05959', '#365959', '#009', '#cc8', '#f65', '#F59'],
  },
  onLoad: function (options) {
    this.setData({
      goodsId: options.goodsId
    })
    this.getComment()
  },
  getComment() {
    let params = {
      goodsId: this.data.goodsId
    }
    App.HttpService.getCommentGoods(params).then(res => {
      console.log(res)
      WxParse.wxParse('article', 'html', res.data.data.propertyContent, this, 0)
      this.pushLabel(res.data.data.labelList)

      this.setData({
        goods: res.data.data,
      })
    })
  },
  toFaviorte() {
    console.log(this.data.goods)
    let params = {
      pointId: this.data.goods.yanGoodsId
    }
    App.HttpServiceNoToast.putFavorite(params).then(res => {
      if(res.data.code=="1000"){
        this.setData({
          'goods.isFavorite': this.data.goods.isFavorite==1?0:1
        })
      }
    })
  },
  pushLabel(labelList) {
    if (labelList) {
      var labelArray = []
      labelList.forEach(n => {
        var index = labelList.indexOf(n);
        this.getLabel(n, index, labelArray)
      })
      this.setData({
        labelArray: labelArray
      })
    }
  },

  getLabel(name, index, labelArray) {
    let top = Math.ceil(Math.random() * 80)
    let left = Math.ceil(Math.random() * 300)
    var that = this
    let isCall = false
    labelArray.forEach(n => {
      if (Math.abs(top - n.top) < 5 || Math.abs(left - n.left) < 10) {
        isCall = true
      }
    })
    if (isCall) {
      return that.getLabel(name, index, labelArray)
    }
    labelArray.push({
      top: top,
      left: left,
      name: name,
      color: index <= 5 ? this.data.colorList[index] : this.data.colorList[Math.ceil(Math.random() * 5)]
    })
  },


  onShow() {
    this.setData({
      'goods.list': [],
      'goods.paginate': {}
    })
    this.getComment()
  },
  gotoComment: function (e) {
    console.log(e.currentTarget.dataset.goodsid)
    App.WxService.navigateTo('/pages/comment/detail/detail?goodsId=' + e.currentTarget.dataset.goodsid)
  },

  putLikeComment: function (n) {
    if (n.isZan == 1) {
      n.isZan = 0
      n.likeCount--
    } else {
      console.log(n)
      n.isZan = 1
      n.likeCount++
    }
    let params = {
      pointId: n.commentId,
      userId: App.getUserId(),
      isValid: n.isZan
    }
    App.HttpServiceNoToast.putLikeComment(params).then(res => {
      console.log(res)
      if (res.data.code == "1000") {

        this.setData({
          'goods.list': this.data.goods.list
        })
      }
    })
  },

  toZan: function (e) {
    console.log(e.target.dataset.commentid)
    let commentId = e.target.dataset.commentid
    this.data.goods.list.forEach(n => {
      if (n.commentId == commentId) {
        this.putLikeComment(n)
      }
    })

  },
  onReady: function () {
    console.log('onReady')
    this.table = this.selectComponent("#table");
  },
  _clickTab: function () {
    let index = this.table.data.currentTab
    this.setData({
      currentTab: index
    })
  },
  swithTab: function (e) {
    this.table.swiperTab(e)
  },
  onShareAppMessage: function () {
    console.log('onShareAppMessage')
  },

  onReachBottom() {
    if (!this.data.goods.paginate.hasNext) {
      return
    }
    let params = {
      goodsId: this.data.goods.yanGoodsId,
      page: this.data.goods.paginate.next,
      pageSize: this.data.goods.paginate.pages,
    }
    App.HttpService.getCommentByCommentId(params).then(res => {
      if (this.data.goods.list) {
        this.data.goods.list = [...this.data.goods.list, ...res.data.data.list]
      }
      this.setData({
        'goods.list': this.data.goods.list,
        'goods.paginate': res.data.data.paginate
      })
    })
  },
})
